package DAO;

import com.google.gson.*;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import vo.News;

import java.io.*;
import java.util.ArrayList;

public class FileDAO {
    public static ArrayList<News> ReadCSV(String filePath){//CSV文件读入
        ArrayList<News> newsList = new ArrayList<>();
        try {
            BufferedReader reader = new BufferedReader(new FileReader(filePath));
            String line;
            while ((line = reader.readLine()) != null) {
                String[] data = line.split(",");
                News news = new News();
                news.setId(Integer.parseInt(data[0]) + 100);//1开头为XLS读入
                news.setTitle(data[1]);
                news.setAddress(data[2]);
                news.setAuthor(data[3]);
                news.setFabushijian(data[4]);
                news.setClick(Integer.parseInt(data[5]));
                newsList.add(news);
            }
            System.out.println("文件导入成功！");
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return newsList;
    }

    public static void WriteCSV(ArrayList<News> list, String filePath){//CSV文件写入
        try {
            FileWriter writer = new FileWriter(filePath);
            for (News news : list) {
                writer.write(news.getId() + "," +
                        news.getTitle() + "," +
                        news.getAddress() + "," +
                        news.getAuthor() + "," +
                        news.getFabushijian() + "," +
                        news.getClick() + "\n");
            }
            writer.close();
            System.out.println("CSV文件写入成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static ArrayList<News> ReadXLS(String filePath){//XLS文件读入
        ArrayList<News> newsList = new ArrayList<>();
        try {
            Workbook workbook = Workbook.getWorkbook(new File(filePath));
            Sheet sheet = workbook.getSheet(0);
            for (int i = 0; i < sheet.getRows(); i++) {
                Cell[] row = sheet.getRow(i);
                News news = new News();
                news.setId(Integer.parseInt(row[0].getContents()) + 200);//2开头为XLS读入
                news.setTitle(row[1].getContents());
                news.setAddress(row[2].getContents());
                news.setAuthor(row[3].getContents());
                news.setFabushijian(row[4].getContents());
                news.setClick(Integer.parseInt(row[5].getContents()));
                newsList.add(news);
            }
            System.out.println("文件导入成功！");
            workbook.close();
        } catch (IOException | BiffException e) {
            e.printStackTrace();
        }
        return newsList;
    }

    public static void WriteXLS (ArrayList<News> list, String filePath){//XLS文件写入
        try {
            WritableWorkbook workbook = Workbook.createWorkbook(new File(filePath));
            WritableSheet sheet = workbook.createSheet("News", 0);
            for (int i = 0; i < list.size(); i++) {
                News news = list.get(i);
                Label idLabel = new Label(0, i, String.valueOf(news.getId()));
                Label titleLabel = new Label(1, i, news.getTitle());
                Label addressLabel = new Label(2, i, news.getAddress());
                Label authorLabel = new Label(3, i, news.getAuthor());
                Label fabushijianLabel = new Label(4, i, news.getFabushijian());
                Label clickLabel = new Label(5, i, String.valueOf(news.getClick()));
                sheet.addCell(idLabel);
                sheet.addCell(titleLabel);
                sheet.addCell(addressLabel);
                sheet.addCell(authorLabel);
                sheet.addCell(fabushijianLabel);
                sheet.addCell(clickLabel);
            }
            workbook.write();
            workbook.close();
            System.out.println("XLS文件写入成功");
        } catch (IOException | WriteException e) {
            e.printStackTrace();
        }
    }
    public static ArrayList<News> ReadFromJson(String filePath){//Json文件写入
        ArrayList<News> newsList = new ArrayList<>();
        try {
            JsonParser parser = new JsonParser();
            FileReader fileReader = new FileReader(filePath);
            JsonElement jsonElement = parser.parse(fileReader);
            JsonArray jsonArray = jsonElement.getAsJsonArray();

            Gson gson = new Gson();
            for (JsonElement element : jsonArray) {
                JsonObject jsonObject = element.getAsJsonObject();
                News news = gson.fromJson(jsonObject, News.class);
                news.setId(news.getId() + 300);//3开头为Json读入
                newsList.add(news);
            }
            System.out.println("文件导入成功！");
            fileReader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return newsList;
    }
}
